package com.gimi.cloud.jimi.common.utils;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import java.util.*;

/**
 * Created by shazhiwei on 2017/12/22.
 */
public class JsonUtil {
    private static Gson gson = null;
    private static JSONObject json = null;
    private static JSONArray jsonArray = null;
    static {
        json = new JSONObject();
        jsonArray = new JSONArray();
        if (gson == null) {
            //gson = new Gson();
            gson = new GsonBuilder().disableHtmlEscaping().create();
        }
    }

    private JsonUtil() {
    }


    /**
     * 将对象转换成json格式
     * @param ts
     * @return
     */
    public static String toJson(Object ts) {
        return JSON.toJSONString(ts);
    }

    /**
     * 将对象转换成json格式(日期处理成yyyy-MM-dd HH:mm:ss)
     * @param ts
     * @return
     */
    public static String toJSONStringWithDateFormatYYYYMMDDHHMMSS(Object ts) {
        String  DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
        return JSON.toJSONStringWithDateFormat(ts,DEFFAULT_DATE_FORMAT);
    }


    /**
     * 将json转换成vo
     * @param jsonStr
     * @param cl
     * @return
     */
    public static Object netJsonToBean(String jsonStr,Class<?> cl){
        net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(jsonStr);
        return net.sf.json.JSONObject.toBean(jsonObject,cl);
    }

    /**
     * 将json转换成bean对象
     * @param jsonStr
     * @return
     */
    public static <T> T jsonToBean(String jsonStr, Class<T> cl) {
        T obj = null;
        if (json != null) {
            obj = JSON.parseObject(jsonStr,cl);
        }
        return obj;
    }

    public static  String replaceNow(String jsonstr){
        return jsonstr.replace("\n","")
                .replace("\t","")
                .replace("\r","")
                .replace("\r\n","")
                .replace(" ","");
    }
    public static Map<String, Object> parseJSON2Map(String jsonStr){
        Map<String, Object> map = new HashMap<String, Object>();
        //最外层解析
        JSONObject json = JSONObject.fromObject(jsonStr);
        for(Object k : json.keySet()){
            Object v = json.get(k);
            //如果内层还是数组的话，继续解析
            if(v instanceof JSONArray){
                List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
                Iterator<JSONObject> it = ((JSONArray)v).iterator();
                while(it.hasNext()){
                    JSONObject json2 = it.next();
                    list.add(parseJSON2Map(json2.toString()));
                }
                map.put(k.toString(), list);
            } else {
                map.put(k.toString(), v);
            }
        }
        return map;
    }
    /**
     *
     * 将json格式转换成map对象
     * @param jsonStr
     * @return
     */
    public static Map<String,String> jsonToMap(String jsonStr) {
        Map<String,String> objMap = null;
        if (gson != null) {
            java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<Map<String,String>>() {
            }.getType();
            objMap = gson.fromJson(jsonStr, type);
        }
        return objMap;
    }

    /**
     * map转成json字符串
     * @param params
     * @return
     * @throws JSONException
     */
    public static String buildJSON(Map<String, Object> params)
            throws JSONException {
        JSONObject jsonObject = new JSONObject();
        for (Map.Entry param : params.entrySet()) {
            if ((param.getKey() != null) && (!"".equals(param.getKey()))) {
                jsonObject.put((String) param.getKey(),
                        param.getValue() == null ? "" : param.getValue());
            }
        }
        return jsonObject.toString();
    }


    /**
     * 用的alibaba的json
     * 根据JSONArray String获取到List
     * @param <T>
     * @param <T>
     * @param jArrayStr
     * @return
     */
    public static <T> List<T> getListByArray(Class<T> class1,String jArrayStr) {
        List<T> list = new ArrayList<>();
        com.alibaba.fastjson.JSONArray jsonArray = com.alibaba.fastjson.JSONArray.parseArray(jArrayStr);
        if (jsonArray==null || jsonArray.isEmpty()) {
            return list;//nerver return null
        }
        for (Object object : jsonArray) {
            com.alibaba.fastjson.JSONObject jsonObject = (com.alibaba.fastjson.JSONObject) object;
            T t = com.alibaba.fastjson.JSONObject.toJavaObject(jsonObject, class1);
            list.add(t);
        }
        return list;
    }

    /**
     * 用的alibaba的json
     * 根据List获取到对应的JSONArray
     * @param list
     * @return
     */
    public static com.alibaba.fastjson.JSONArray getJSONArrayByList(List<?> list){
        com.alibaba.fastjson.JSONArray jsonArray = new com.alibaba.fastjson.JSONArray();
        if (list==null ||list.isEmpty()) {
            return jsonArray;//nerver return null
        }
        for (Object object : list) {
            jsonArray.add(object);
        }
        return jsonArray;
    }
}
